#============================================================================
import sys
+import time
+import re
sys.path.append('/usr/lib/python')
from xen.util.xmlrpclib2 import ServerProxy
'%(type)-10s'
VDI_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(virtual_size)-8s '\
'%(sector_size)-8s'
-VBD_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(VDI)-8s '\
- '%(image)-8s'
+VBD_LIST_FORMAT = '%(device)-6s %(uuid)-36s %(VDI)-8s %(image)-8s'
+TASK_LIST_FORMAT = '%(name_label)-18s %(uuid)-36s %(status)-8s %(progress)-4s'
+VIF_LIST_FORMAT = '%(name)-8s %(device)-7s %(uuid)-36s %(MAC)-10s'
COMMANDS = {
'host-info': ('', 'Get Xen Host Info'),
'vdi-create': ('<pycfg> [opts]', 'Create a VDI'),
'vdi-list' : ('', 'List all VDI'),
'vdi-rename': ('<vdi_uuid> <new_name>', 'Rename VDI'),
- 'vdi-delete': ('<vdi_uuid>', 'Delete VDI'),
+ 'vdi-destroy': ('<vdi_uuid>', 'Delete VDI'),
'vif-create': ('<domname> <pycfg>', 'Create VIF attached to domname'),
'vtpm-create' : ('<domname> <pycfg>', 'Create VTPM attached to domname'),
'vm-shutdown': ('<name> [opts]', 'Shutdown VM with name'),
'vm-start': ('<name>', 'Start VM with name'),
'vm-uuid': ('<name>', 'UUID of a domain by name.'),
+ 'async-vm-start': ('<name>', 'Start VM asynchronously'),
}
OPTIONS = {
values = defaults)
return opts, extraargs
-def execute(fn, *args):
- result = fn(*args)
+def execute(server, fn, args, async = False):
+ if async:
+ func = eval('server.Async.%s' % fn)
+ else:
+ func = eval('server.%s' % fn)
+
+ result = func(*args)
if type(result) != DictType:
raise TypeError("Function returned object of type: %s" %
str(type(result)))
_initialised = False
_server = None
_session = None
-def _connect(*args):
+def connect(*args):
global _server, _session, _initialised
if not _initialised:
- _server = ServerProxy('httpu:///var/run/xend/xen-api.sock')
+ _server = ServerProxy('http://localhost:9363/')
login = raw_input("Login: ")
password = getpass()
creds = (login, password)
- _session = execute(_server.session.login_with_password, *creds)
+ _session = execute(_server.session, 'login_with_password', creds)
_initialised = True
return (_server, _session)
return cfg
def resolve_vm(server, session, vm_name):
- vm_uuid = execute(server.VM.get_by_name_label, session, vm_name)
+ vm_uuid = execute(server, 'VM.get_by_name_label', (session, vm_name))
if not vm_uuid:
return None
else:
return vm_uuid[0]
def resolve_vdi(server, session, vdi_name):
- vdi_uuid = execute(server.VDI.get_by_name_label, session, vdi_name)
+ vdi_uuid = execute(server, 'VDI.get_by_name_label', (session, vdi_name))
if not vdi_uuid:
return None
else:
# Actual commands
#
-def xapi_host_info(*args):
- server, session = _connect()
- hosts = execute(server.host.get_all, session)
+def xapi_host_info(args, async = False):
+ server, session = connect()
+ hosts = execute(server, 'host.get_all', (session,))
for host in hosts: # there is only one, but ..
- hostinfo = execute(server.host.get_record, session, host)
+ hostinfo = execute(server, 'host.get_record', (session, host))
print HOST_INFO_FORMAT % ('Name', hostinfo['name_label'])
print HOST_INFO_FORMAT % ('Version', hostinfo['software_version'])
print HOST_INFO_FORMAT % ('CPUs', len(hostinfo['host_CPUs']))
print HOST_INFO_FORMAT % ('UUID', host)
for host_cpu_uuid in hostinfo['host_CPUs']:
- host_cpu = execute(server.host_cpu.get_record, session,
- host_cpu_uuid)
+ host_cpu = execute(server, 'host_cpu.get_record',
+ (session, host_cpu_uuid))
print 'CPU %s Util: %.2f' % (host_cpu['number'],
float(host_cpu['utilisation']))
-def xapi_host_set_name(*args):
+def xapi_host_set_name(args, async = False):
if len(args) < 1:
raise OptionError("No hostname specified")
- server, session = _connect()
- hosts = execute(server.host.get_all, session)
+ server, session = connect()
+ hosts = execute(server, 'host.get_all', (session,))
if len(hosts) > 0:
- execute(server.host.set_name_label, session, hosts[0], args[0])
- print 'Hostname: %s' % execute(server.host.get_name_label, session,
- hosts[0])
+ execute(server, 'host.set_name_label', (session, hosts[0], args[0]))
+ print 'Hostname: %s' % execute(server, 'host.get_name_label',
+ (session, hosts[0]))
-def xapi_vm_uuid(*args):
+def xapi_vm_uuid(args, async = False):
if len(args) < 1:
raise OptionError("No domain name specified")
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, args[0])
print vm_uuid
-def xapi_vm_name(*args):
+def xapi_vm_name(args, async = False):
if len(args) < 1:
raise OptionError("No UUID specified")
- server, session = _connect()
- vm_name = execute(server.VM.get_name_label, session, args[0])
+ server, session = connect()
+ vm_name = execute(server, 'VM.get_name_label', (session, args[0]))
print vm_name
-def xapi_vm_list(*args):
+def xapi_vm_list(args, async = False):
opts, args = parse_args('vm-list', args, set_defaults = True)
is_long = opts and opts.long
- server, session = _connect()
- vm_uuids = execute(server.VM.get_all, session)
+ server, session = connect()
+ vm_uuids = execute(server, 'VM.get_all', (session,))
if not is_long:
print VM_LIST_FORMAT % {'name_label':'Name',
'memory_actual':'Mem',
'uuid': 'UUID'}
for uuid in vm_uuids:
- vm_info = execute(server.VM.get_record, session, uuid)
+ vm_info = execute(server, 'VM.get_record', (session, uuid))
if is_long:
vbds = vm_info['VBDs']
vifs = vm_info['VIFs']
vbd_infos = []
vtpm_infos = []
for vbd in vbds:
- vbd_info = execute(server.VBD.get_record, session, vbd)
+ vbd_info = execute(server, 'VBD.get_record', (session, vbd))
vbd_infos.append(vbd_info)
for vif in vifs:
- vif_info = execute(server.VIF.get_record, session, vif)
+ vif_info = execute(server, 'VIF.get_record', (session, vif))
vif_infos.append(vif_info)
for vtpm in vtpms:
- vtpm_info = execute(server.VTPM.get_record, session, vtpm)
+ vtpm_info = execute(server, 'VTPM.get_record', (session, vtpm))
vtpm_infos.append(vtpm_info)
vm_info['VBDs'] = vbd_infos
vm_info['VIFs'] = vif_infos
else:
print VM_LIST_FORMAT % _stringify(vm_info)
-def xapi_vm_create(*args):
+def xapi_vm_create(args, async = False):
if len(args) < 1:
raise OptionError("Configuration file not specified")
cfg = _read_python_cfg(filename)
print 'Creating VM from %s ..' % filename
- server, session = _connect()
- uuid = execute(server.VM.create, session, cfg)
+ server, session = connect()
+ uuid = execute(server, 'VM.create', (session, cfg), async = async)
print 'Done. (%s)' % uuid
print uuid
-def xapi_vm_destroy(*args):
+def xapi_vm_destroy(args, async = False):
if len(args) < 1:
raise OptionError("No domain name specified.")
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, args[0])
print 'Destroying VM %s (%s)' % (args[0], vm_uuid)
- success = execute(server.VM.destroy, session, vm_uuid)
+ success = execute(server, 'VM.destroy', (session, vm_uuid), async = async)
print 'Done.'
-def xapi_vm_start(*args):
+def xapi_vm_start(args, async = False):
if len(args) < 1:
raise OptionError("No Domain name specified.")
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, args[0])
print 'Starting VM %s (%s)' % (args[0], vm_uuid)
- success = execute(server.VM.start, session, vm_uuid, False)
- print 'Done.'
+ success = execute(server, 'VM.start', (session, vm_uuid, False), async = async)
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
+
+def xapi_vm_suspend(args, async = False):
+ if len(args) < 1:
+ raise OptionError("No Domain name specified.")
+
+ server, session = connect()
+ vm_uuid = resolve_vm(server, session, args[0])
+ print 'Suspending VM %s (%s)' % (args[0], vm_uuid)
+ success = execute(server, 'VM.suspend', (session, vm_uuid), async = async)
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
+
+
+def xapi_vm_resume(args, async = False):
+ if len(args) < 1:
+ raise OptionError("No Domain name specified.")
+
+ server, session = connect()
+ vm_uuid = resolve_vm(server, session, args[0])
+ print 'Resuming VM %s (%s)' % (args[0], vm_uuid)
+ success = execute(server, 'VM.resume', (session, vm_uuid, False), async = async)
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
+
+def xapi_vm_pause(args, async = False):
+ if len(args) < 1:
+ raise OptionError("No Domain name specified.")
+
+ server, session = connect()
+ vm_uuid = resolve_vm(server, session, args[0])
+ print 'Pausing VM %s (%s)' % (args[0], vm_uuid)
+ success = execute(server, 'VM.pause', (session, vm_uuid), async = async)
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
-def xapi_vm_shutdown(*args):
+def xapi_vm_unpause(args, async = False):
+ if len(args) < 1:
+ raise OptionError("No Domain name specified.")
+
+ server, session = connect()
+ vm_uuid = resolve_vm(server, session, args[0])
+ print 'Pausing VM %s (%s)' % (args[0], vm_uuid)
+ success = execute(server, 'VM.unpause', (session, vm_uuid), async = async)
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
+
+def xapi_task_list(args, async = False):
+ server, session = connect()
+ all_tasks = execute(server, 'task.get_all', (session,))
+
+ print TASK_LIST_FORMAT % {'name_label': 'Task Name',
+ 'uuid': 'UUID',
+ 'status': 'Status',
+ 'progress': '%'}
+
+ for task_uuid in all_tasks:
+ task = execute(server, 'task.get_record', (session, task_uuid))
+ print TASK_LIST_FORMAT % task
+
+def xapi_task_clear(args, async = False):
+ server, session = connect()
+ all_tasks = execute(server, 'task.get_all', (session,))
+ for task_uuid in all_tasks:
+ success = execute(server, 'task.destroy', (session, task_uuid))
+ print 'Destroyed Task %s' % task_uuid
+
+def xapi_vm_shutdown(args, async = False):
opts, args = parse_args("vm-shutdown", args, set_defaults = True)
if len(args) < 1:
raise OptionError("No Domain name specified.")
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, args[0])
if opts.force:
print 'Forcefully shutting down VM %s (%s)' % (args[0], vm_uuid)
- success = execute(server.VM.hard_shutdown, session, vm_uuid)
+ success = execute(server, 'VM.hard_shutdown', (session, vm_uuid), async = async)
else:
print 'Shutting down VM %s (%s)' % (args[0], vm_uuid)
- success = execute(server.VM.clean_shutdown, session, vm_uuid)
- print 'Done.'
+ success = execute(server, 'VM.clean_shutdown', (session, vm_uuid), async = async)
-def xapi_vbd_create(*args):
+ if async:
+ print 'Task started: %s' % success
+ else:
+ print 'Done.'
+
+def xapi_vbd_create(args, async = False):
opts, args = parse_args('vbd-create', args)
if len(args) < 2:
cfg[opt] = val
print 'Creating VBD ...',
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, domname)
cfg['VM'] = vm_uuid
- vbd_uuid = execute(server.VBD.create, session, cfg)
- print 'Done. (%s)' % vbd_uuid
+ vbd_uuid = execute(server, 'VBD.create', (session, cfg), async = async)
+ if async:
+ print 'Task started: %s' % vbd_uuid
+ else:
+ print 'Done. (%s)' % vbd_uuid
-def xapi_vif_create(*args):
+def xapi_vif_create(args, async = False):
if len(args) < 2:
raise OptionError("Configuration file not specified")
cfg = _read_python_cfg(filename)
print 'Creating VIF from %s ..' % filename
- server, session = _connect()
+ server, session = connect()
vm_uuid = resolve_vm(server, session, domname)
cfg['VM'] = vm_uuid
- vif_uuid = execute(server.VIF.create, session, cfg)
- print 'Done. (%s)' % vif_uuid
+ vif_uuid = execute(server, 'VIF.create', (session, cfg), async = async)
+ if async:
+ print 'Task started: %s' % vif_uuid
+ else:
+ print 'Done. (%s)' % vif_uuid
-def xapi_vbd_list(*args):
- server, session = _connect()
+def xapi_vbd_list(args, async = False):
+ server, session = connect()
domname = args[0]
dom_uuid = resolve_vm(server, session, domname)
- vbds = execute(server.VM.get_VBDs, session, dom_uuid)
+ vbds = execute(server, 'VM.get_VBDs', (session, dom_uuid))
- print VBD_LIST_FORMAT % {'name_label': 'VDI Label',
+ print VBD_LIST_FORMAT % {'device': 'Device',
'uuid' : 'UUID',
'VDI': 'VDI',
'image': 'Image'}
for vbd in vbds:
- vbd_struct = execute(server.VBD.get_record, session, vbd)
+ vbd_struct = execute(server, 'VBD.get_record', (session, vbd))
print VBD_LIST_FORMAT % vbd_struct
+
+def xapi_vif_list(args, async = False):
+ server, session = connect()
+ domname = args[0]
+
+ dom_uuid = resolve_vm(server, session, domname)
+ vifs = execute(server, 'VM.get_VIFs', (session, dom_uuid))
+
+ print VIF_LIST_FORMAT % {'name': 'Name',
+ 'device': 'Device',
+ 'uuid' : 'UUID',
+ 'MAC': 'MAC'}
+
+ for vif in vifs:
+ vif_struct = execute(server, 'VIF.get_record', (session, vif))
+ print VIF_LIST_FORMAT % vif_struct
-def xapi_vdi_list(*args):
+def xapi_vdi_list(args, async = False):
opts, args = parse_args('vdi-list', args, set_defaults = True)
is_long = opts and opts.long
- server, session = _connect()
- vdis = execute(server.VDI.get_all, session)
+ server, session = connect()
+ vdis = execute(server, 'VDI.get_all', (session,))
if not is_long:
print VDI_LIST_FORMAT % {'name_label': 'VDI Label',
'sector_size': 'Sector Size'}
for vdi in vdis:
- vdi_struct = execute(server.VDI.get_record, session, vdi)
+ vdi_struct = execute(server, 'VDI.get_record', (session, vdi))
print VDI_LIST_FORMAT % vdi_struct
else:
-
for vdi in vdis:
- vdi_struct = execute(server.VDI.get_record, session, vdi)
+ vdi_struct = execute(server, 'VDI.get_record', (session, vdi))
pprint(vdi_struct)
-def xapi_sr_list(*args):
+def xapi_sr_list(args, async = False):
opts, args = parse_args('sr-list', args, set_defaults = True)
is_long = opts and opts.long
- server, session = _connect()
- srs = execute(server.SR.get_all, session)
+ server, session = connect()
+ srs = execute(server, 'SR.get_all', (session,))
if not is_long:
print SR_LIST_FORMAT % {'name_label': 'SR Label',
'uuid' : 'UUID',
'type': 'Type'}
for sr in srs:
- sr_struct = execute(server.SR.get_record, session, sr)
+ sr_struct = execute(server, 'SR.get_record', (session, sr))
sr_struct['physical_size'] = int(sr_struct['physical_size'])/MB
print SR_LIST_FORMAT % sr_struct
else:
for sr in srs:
- sr_struct = execute(server.SR.get_record, session, sr)
+ sr_struct = execute(server, 'SR.get_record', (session, sr))
pprint(sr_struct)
-def xapi_sr_rename(*args):
- server, session = _connect()
- sr = execute(server.SR.get_by_name_label, session, args[0])
- execute(server.SR.set_name_label, session, sr[0], args[1])
+def xapi_sr_rename(args, async = False):
+ server, session = connect()
+ sr = execute(server, 'SR.get_by_name_label', (session, args[0]))
+ execute(server, 'SR.set_name_label', (session, sr[0], args[1]))
-def xapi_vdi_create(*args):
+def xapi_vdi_create(args, async = False):
opts, args = parse_args('vdi-create', args)
if len(args) > 0:
for opt, val in opts:
cfg[opt] = val
- server, session = _connect()
- srs = execute(server.SR.get_all, session)
+ server, session = connect()
+ srs = execute(server, 'SR.get_all', (session,))
sr = srs[0]
cfg['SR'] = sr
size = (cfg['virtual_size'] * cfg['sector_size'])/MB
print 'Creating VDI of size: %dMB ..' % size,
- uuid = execute(server.VDI.create, session, cfg)
- print 'Done. (%s)' % uuid
+ uuid = execute(server, 'VDI.create', (session, cfg), async = async)
+ if async:
+ print 'Task started: %s' % uuid
+ else:
+ print 'Done. (%s)' % uuid
+
-def xapi_vdi_delete(*args):
- server, session = _connect()
+def xapi_vdi_destroy(args, async = False):
+ server, session = connect()
if len(args) < 1:
raise OptionError('Not enough arguments')
vdi_uuid = args[0]
print 'Deleting VDI %s' % vdi_uuid
- result = execute(server.VDI.destroy, session, vdi_uuid)
- print 'Done.'
+ result = execute(server, 'VDI.destroy', (session, vdi_uuid), async = async)
+ if async:
+ print 'Task started: %s' % result
+ else:
+ print 'Done.'
-def xapi_vdi_rename(*args):
- server, session = _connect()
+def xapi_vdi_rename(args, async = False):
+ server, session = connect()
if len(args) < 2:
raise OptionError('Not enough arguments')
- vdi_uuid = execute(server.VDI.get_by_name_label, session, args[0])
+ vdi_uuid = execute(server, 'VDI.get_by_name_label', session, args[0])
vdi_name = args[1]
print 'Renaming VDI %s to %s' % (vdi_uuid[0], vdi_name)
- result = execute(server.VDI.set_name_label, session, vdi_uuid[0], vdi_name)
- print 'Done.'
+ result = execute(server, 'VDI.set_name_label',
+ (session, vdi_uuid[0], vdi_name), async = async)
+ if async:
+ print 'Task started: %s' % result
+ else:
+ print 'Done.'
+
-def xapi_vtpm_create(*args):
- server, session = _connect()
+def xapi_vtpm_create(args, async = False):
+ server, session = connect()
domname = args[0]
cfg = _read_python_cfg(args[1])
vm_uuid = resolve_vm(server, session, domname)
cfg['VM'] = vm_uuid
print "Creating vTPM with cfg = %s" % cfg
- vtpm_uuid = execute(server.VTPM.create, session, cfg)
+ vtpm_uuid = execute(server, 'VTPM.create', (session, cfg))
print "Done. (%s)" % vtpm_uuid
- vtpm_id = execute(server.VTPM.get_instance, session, vtpm_uuid)
+ vtpm_id = execute(server, 'VTPM.get_instance', (session, vtpm_uuid))
print "Has instance number '%s'" % vtpm_id
- vtpm_be = execute(server.VTPM.get_backend, session, vtpm_uuid)
+ vtpm_be = execute(server, 'VTPM.get_backend', (session, vtpm_uuid))
print "Has backend in '%s'" % vtpm_be
- driver = execute(server.VTPM.get_driver, session, vtpm_uuid)
+ driver = execute(server, 'VTPM.get_driver', (session, vtpm_uuid))
print "Has driver type '%s'" % driver
- vtpm_rec = execute(server.VTPM.get_record, session, vtpm_uuid)
+ vtpm_rec = execute(server, 'VTPM.get_record', (session, vtpm_uuid))
print "Has vtpm record '%s'" % vtpm_rec
-def xapi_pif_list(*args):
- server, session = _connect()
- pif_uuids = execute(server.PIF.get_all, session)
+def xapi_pif_list(args, async = False):
+ server, session = connect()
+ pif_uuids = execute(server, 'PIF.get_all', (session,))
for pif_uuid in pif_uuids:
- pif = execute(server.PIF.get_record, session, pif_uuid)
+ pif = execute(server, 'PIF.get_record', (session, pif_uuid))
print pif
-
+
+
+def xapi_debug_wait(args, async = False):
+ secs = 10
+ if len(args) > 0:
+ secs = int(args[0])
+ server, session = connect()
+ task_uuid = execute(server, 'Debug.wait', (session, secs), async=async)
+ print 'Task UUID: %s' % task_uuid
#
# Command Line Utils
words = shlex.split(line)
if len(words) > 0:
cmd_name = words[0].replace('-', '_')
+ is_async = 'async' in cmd_name
+ if is_async:
+ cmd_name = re.sub('async_', '', cmd_name)
+
func_name = 'xapi_%s' % cmd_name
func = globals().get(func_name)
+
if func:
try:
args = tuple(words[1:])
- func(*args)
+ func(args, async = is_async)
return True
except SystemExit:
return False
return line
def shell():
- server, session = _connect()
+ server, session = connect()
x = XenAPICmd(server, session)
x.cmdloop('Xen API Prompt. Type "help" for a list of functions')
usage()
sys.exit(1)
- subcmd = args[0]
- subcmd_func_name = 'xapi_' + subcmd.replace('-', '_')
+ subcmd = args[0].replace('-', '_')
+ is_async = 'async' in subcmd
+ if is_async:
+ subcmd = re.sub('async_', '', subcmd)
+ subcmd_func_name = 'xapi_' + subcmd
subcmd_func = globals().get(subcmd_func_name, None)
if subcmd == 'shell':
sys.exit(1)
try:
- subcmd_func(*args[1:])
+ subcmd_func(args[1:], async = is_async)
except XenAPIError, e:
print 'Error: %s' % str(e.args[0])
sys.exit(2)